package atm.com.util;

import atm.com.pojo.*;
import atm.com.service.bankcard.BankCardServiceInf;
import atm.com.service.journal.JournalServiceInf;
import atm.com.service.stream.StreamServiceInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * @author  di
 * @date  2021/8/26
 * */
@Component
public class MethodUtil {
    @Autowired
    private JournalServiceInf journalServiceInf;
    private static JournalServiceInf journalInf;

    @Autowired
    private BankCardServiceInf bankCardServiceInf;
    private static BankCardServiceInf bankCardInf;
    @Autowired
    private StreamServiceInf streamServiceInf;
    private static  StreamServiceInf streamInf;

    @PostConstruct
    public void init() {
        journalInf=this.journalServiceInf;
        bankCardInf=this.bankCardServiceInf;
        streamInf=this.streamServiceInf;
    }


    public  void InsertJournal(HttpSession session,String operate){
        CommonUtil commonUtil=new CommonUtil();
        Journal journal =new Journal();
        journal.setUserId(((User)session.getAttribute(commonUtil.USER)).getUserId());
        journal.setCashDispenserId(((Cashdispenser)session.getAttribute(commonUtil.CASH_DISPENSER)).getCashDispenserId());
        journal.setBankCardId(((Bankcard)session.getAttribute(commonUtil.BANK_CARD)).getBankcardId());
        journal.setJournalOperate(operate);
        journalInf.InsertJournal(journal);
    }

    public void InsertJournal(HttpSession session,String operate,BigDecimal money){
        CommonUtil commonUtil=new CommonUtil();
        Journal journal =new Journal();
        journal.setUserId(((User)session.getAttribute(commonUtil.USER)).getUserId());
        journal.setCashDispenserId(((Cashdispenser)session.getAttribute(commonUtil.CASH_DISPENSER)).getCashDispenserId());
        journal.setJournalOperate(operate);
            journalInf.InsertJournal(journal);
        journal.setBankCardId(((Bankcard)session.getAttribute(commonUtil.BANK_CARD)).getBankcardId());
        Stream stream =new Stream();
        stream.setUserId(journal.getUserId());
        stream.setBankcardId(((Bankcard)session.getAttribute(commonUtil.BANK_CARD)).getBankcardId());
        stream.setStreamRecord(journal.getJournalOperate());
        stream.setStreamMoney(money);
        stream.setStreamBalance((bankCardInf.GetBanCardBalance(((Bankcard)session.getAttribute(commonUtil.BANK_CARD)))));
            streamInf.InsertStream(stream);
    }
}
